﻿// <copyright file="ListViewCustomComparer.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// The abstract listview camparer class
    /// </summary>
    /// <typeparam name="T">The type being compared</typeparam>
    public abstract class ListViewCustomComparer<T> : IComparer, IListViewCustomComparer where T : class
    {
        #region Fields

        /// <summary>
        /// The column name to sort by
        /// </summary>
        private string sortBy = string.Empty;
        
        /// <summary>
        /// The sort direction
        /// </summary>
        private ListSortDirection direction = ListSortDirection.Ascending;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the sort by data column name.
        /// </summary>
        /// <value>The sort by.</value>
        public string SortBy
        {
            get { return this.sortBy; }
            set { this.sortBy = value; }
        }

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        /// <value>The sort direction.</value>
        public ListSortDirection SortDirection
        {
            get { return this.direction; }
            set { this.direction = value; }
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other
        /// </summary>
        /// <param name="x">The first object to compare</param>
        /// <param name="y">The second object to compare</param>
        /// <returns>
        /// Value Condition Less than zero <paramref name="x"/> is less than <paramref name="y"/>. Zero <paramref name="x"/> equals <paramref name="y"/>. Greater than zero <paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">Neither <paramref name="x"/> nor <paramref name="y"/> implements the <see cref="T:System.IComparable"/> interface.-or- <paramref name="x"/> and <paramref name="y"/> are of different types and neither one can handle comparisons with the other. </exception>
        public int Compare(object x, object y)
        {
            T item1 = x as T;
            T item2 = y as T;

            if (item1 == null || item2 == null)
            {
                System.Diagnostics.Trace.Write("either x or y is null in compare(x,y)");
                return 0;
            }

            return this.Compare(item1, item2);
        }

        /// <summary>
        /// Compares the specified x to y
        /// </summary>
        /// <param name="x">The x value to compare</param>
        /// <param name="y">The y value to compare</param>
        /// <returns>The comparison result</returns>
        public abstract int Compare(T x, T y);

        #endregion
    }
}
